using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 维护本层UI
/// </summary>
public class UiLayer
{
    public int layerType;

    private List<UiView> viewList = new List<UiView>();



    public void AddView(UiView uiView)
    {
        if (uiView!=null)
        {
            if (!viewList.Contains(uiView))
            {
                viewList.Add(uiView);
                RearrangeSortingOrder();
            }
        }
    }

    public UiView GetView(int viewId)
    {
        int uiViewCount = viewList.Count;
        for (int i = 0; i < uiViewCount; i++)
        {
            UiView uiView = viewList[i];
            if (uiView!=null&&uiView.GetViewId()==viewId)
            {
                return uiView;
            }
        }

        return null;
    }

    public int GetViewCount()
    {
        int viewCount = 0;
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i] != null && viewList[i].gameObject.activeSelf)
            {
                ++viewCount;
            }
        }
        return viewCount;
    }

    public void RemoveView(int viewId)
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i]!=null&&viewList[i].GetViewId()==viewId)
            {
                viewList[i].SetViewAcitve(false);
                viewList.RemoveAt(i);
                return;
            }
        }
    }

    public void RemoveView(UiView uiView)
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i]!=null)
            {
                RemoveView(viewList[i].GetViewId());
            }
        }
    }

    public bool HasView(int viewId)
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i]!=null&&viewList[i].GetViewId()==viewId)
            {
                return true;
            }
        }

        return false;
    }

    public void SetLayerActive(bool active)
    {
        throw new NotImplementedException();
    }

    public void ClearLayer()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// �Ż��رղ�
    /// </summary>
    public void OptimizeCloseLayer()
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i]!=null&&viewList[i].IsLarget)
            {
                viewList[i].OnHide();
            }
            else
            {
                viewList[i].OnRemove();
            }

        }
    }

    /// <summary>
    /// 排序后打开的放上面
    /// </summary>
    public void RearrangeSortingOrder()
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            if (viewList[i]!=null)
            {
                int layerType = viewList[i].LayerType;
                viewList[i].SortingOrder = layerType + i * 10;
            }
        }
    }
}
